package com.api.controller;

import com.api.mapper.BuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    BuildingMapper bm;

    @PostMapping("/create")
    public Map<String, Object> create(String label) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        bm.create(label);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @DeleteMapping("/remove")
    public Map<String, Object> remove(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        bm.remove(id);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @PutMapping("/update")
    public Map<String, Object> update(Integer id, String label) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        bm.update(id, label);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @GetMapping("/get")
    public Map<String, Object> get(Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        pageNo = --pageNo * pageSize;
        map.put("data", bm.get(pageNo, pageSize));
        map.put("total", bm.count());
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @GetMapping("/getAll")
    public Map<String, Object> getAll() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("data", bm.getAll());
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
}
